<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="商户名称">
              <a-input v-model="queryParam.name" placeholder="请输入"/>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="上级机构">
              <a-tree-select
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                placeholder='请选择'
                allowClear
                treeDefaultExpandAll
                :treeData="treeData"
                v-model="queryParam.code"
              >
              <span style="color:#08c" slot="title" slot-scope="{key}"
                    v-decorator="['key',{rules: [{ required: true, message: '请选择商户' }]}]">
                {{value}}
              </span>>
              </a-tree-select>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:query style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button v-action:add type="primary" icon="plus" @click="$refs.modal.add()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :scroll="{x:1100}"
    >
      <a-tag  v-if="text==false" slot="state" slot-scope="text" color="#f50">
        {{ text | statusFilter }}
      </a-tag>
      <a-tag   v-else slot="state" slot-scope="text" color="#2db7f5">
        {{ text | statusFilter }}
      </a-tag>
      <span slot="drawFee" slot-scope="text">
        {{ text / 100 }}
      </span>
      <template slot-scope="text" slot="isRelation">
        <a-tag v-if="text==0" color="#f50">
          {{ text | isRelationStatus }}
        </a-tag>
        <a-tag v-if="text==1" color="#2db7f5">
          {{ text | isRelationStatus }}
        </a-tag>
      </template>

      <span slot="action" slot-scope="text, record">
        <template  style="width:21%">
          <a v-action:update @click="$refs.modal.edit(record)">编辑</a>
          <a-divider type="vertical"/>
        </template>
        <a-dropdown>
          <a class="ant-dropdown-link">
            更多<a-icon type="down"/>
          </a>
          <a-menu slot="overlay">
            <a-menu-item>
<!--              $refs.setModal.Receiver(record,'')-->
               <a v-action:other @click="$refs.setModal.receiver(record,'')">分账管理</a>
            </a-menu-item>
            <a-menu-item>
              <a  @click="$refs.setModal.receiver(record,'device')">二维码</a>
            </a-menu-item>
            <a-menu-item>
              <a  @click="bindGzhAndXcx(record)">关联小程序+公众号</a>
            </a-menu-item>
            <a-menu-item>
              <a v-action:update :href="'http://wechat.alipayjf.com/api/open/authGo'"
              target="_blank">微信授权</a>
            </a-menu-item>
            <a-menu-item>
              <a v-action:delete @click="handleDel(record)">删除</a>
            </a-menu-item>
         </a-menu>
        </a-dropdown>
      </span>
    </s-table>

    <Edit ref="modal" @ok="handleOk"></Edit>
    <Receiver ref="setModal" @ok="handleOk" ></Receiver>
    <Dialog ref="dialog" @ok="handleOk" ></Dialog>
  </a-card>
</template>

<script>
  import STable from '@/components/table/'
  import Edit from './Edit'
  import Receiver from './Receiver'
  import Dialog from './Eldialog'
  import { selectPage, del,bind} from '@/api/merchantService/merchant'
  import { selectTree } from '@/api/storeService/store'
  import axios from 'axios'
  import qs from 'qs'

  export default {
    components: {
      STable,
      Edit,
      Receiver,
      Dialog
    },
    //--############  data() ###############-->
    data() {
      return {
        description: '',
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        //微信菜单
        menuInfo: {},
        configureMenu: false,
        weixinTitle: '公众号菜单',
        //菜单对象
        // menu: {'button': []},//当前菜单
        menu: {
          "button": [
            {
              "name": "主菜单1",
              "sub_button": []
            },
            {
              "name": "主菜单2",
              "sub_button": []
            },
            {
              "name": "主菜单3",
              "sub_button": [
                {
                  "name": "子菜单1"
                }]
            }]
        },

        selectedMenuIndex:'',//当前选中菜单索引
        selectedSubMenuIndex:'',//当前选中子菜单索引
        menuNameBounds:false,//菜单长度是否过长
        material:{
          title:'',
          url:'',
          thumb_url:''
        },
        // 表头
        columns: [
          {
            title: '编号',
            dataIndex: 'code'
          },
          {
            title: '商户名称',
            dataIndex: 'name'
          },
          {
            title: '是否关联',
            dataIndex: 'isRelation',
            scopedSlots: { customRender: 'isRelation' }
          },
          {
            title: '商户APPID',
            dataIndex: 'subAppId'
          },
          {
            title: '公众号微信号',
            dataIndex: 'subAppAccount'
          },
          {
            title: '商户号',
            dataIndex: 'subMchId'
          },
          {
            title: '操作',
            fixed: 'right',
            width: 130,
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return selectPage(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        },
        treeData:[],
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created() {
      const _this = this
      const params = { 'ctrlType': '1', 'displayType': '1,2' }
      /*ctrlType:控制层级结构不能操作，1，服务商，2，代理商，3商户，4，门店
      * displayType:控制层级显示，必须是连续，1，服务商，2，代理商，3商户，4，门店
      * */
      selectTree(params).then(res => {
        if (res.code === 200) {
          if (res.result == null) {
            return
          }
          _this.treeData = res.result
        } else {
          this.$notification.error({
            message: '错误',
            description: res.message,
            duration: 4
          })
        }
      })
    },
    methods: {

      handleDel(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要删除 ${row.name} 吗?`,
          okText: '删除',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            del(row.id).then(res => {
              if (res.code === 200) {
                _this.$message.success('删除成功')
                _this.$emit('ok')
                _this.$refs.table.refresh()
              }
              else {
                _this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            })
          },
          onCancel() {
          }
        })
      },
      handleOk() {
        // 新增/修改 成功时，重载列表
        this.$refs.table.refresh(false)
      },
      bindGzhAndXcx(row){
        bind(row.id).then(res => {
          if (res.code === 200) {
            this.$notification.info({
              message: '成功',
              description: res.message,
              duration: 4
            })
          } else {
            this.$notification.error({
              message: '错误',
              description: res.message,
              duration: 4
            })
          }
        })
      },
      //二维码
      handleCode: function(row) {
        window.location.href = this.$baseURL + '/pc/product/downloadFile?id=' + row.id
      },

    },
    //--############  methods() ###############-->
    //--############  filters() ###############-->
    filters: {
      statusFilter(state) {
        const statusMap = {
          false: '未使用',
          true: '已使用'
        }
        return statusMap[state]
      },
      isRelationStatus(state) {
        const statusMap = {
          0: '未关联',
          1: '已关联'
        }
        return statusMap[state]
      }
    }
    //--############  filters() ###############-->
  }
</script>
<style lang="less" scoped>
  .ant-table-row  :last-child{
    width: 21%;
  }
  *{
    box-sizing: border-box;
  }
  ul{
    padding:0;
  }
  li{
    list-style:none;
  }
  #app-menu{
    overflow: hidden;
    width:950px;
  }
  .weixin-preview{
    position: relative;
    width: 320px;
    height: 540px;
    float: left;
    margin-right:10px;
    border: 1px solid #e7e7eb;
  }
  .weixin-preview a{
    text-decoration: none;
    color: #616161;
  }
  .weixin-preview .weixin-hd{
    color: #fff;
    text-align: center;
    position: relative;
    top:0px;
    left:0px;
    width: 320px;
    height:64px;
    background:transparent url("../../../assets/wechat/menu_head.png") no-repeat 0 0;
    background-position: 0 0;
  }
  .weixin-preview .weixin-hd .weixin-title{
    color:#fff;
    font-size:15px;
    width:100%;
    text-align: center;
    position:absolute;
    top: 33px;
    left: 0px;
  }

  .weixin-preview .weixin-bd{

  }
  .weixin-preview .weixin-menu{
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    border-top: 1px solid #e7e7e7;
    background: transparent url("../../../assets/wechat/menu_foot.png") no-repeat 0 0;
    background-position: 0 0;
    background-repeat: no-repeat;
    padding-left: 43px;
    margin-bottom:0px;
  }
  /*一级*/
  .weixin-preview .weixin-menu .menu-item{
    position: relative;
    float: left;
    line-height: 50px;
    height:50px;
    text-align: center;
    width:33.33%;
    border-left: 1px solid #e7e7e7;
    cursor: pointer;
    color:#616161;
  }
  .weixin-preview .weixin-menu .menu-item:first-child{
    /*border-left-width:0px;*/
  }

  /*二级*/
  .weixin-preview .weixin-sub-menu{
    position: absolute;
    bottom: 60px;
    left: 0;
    right: 0;
    border-top: 1px solid #d0d0d0;
    margin-bottom:0px;
    background: #fafafa;
    display: block;
    padding:0;
  }
  .weixin-preview .weixin-sub-menu .menu-sub-item{
    line-height: 50px;
    height:50px;
    text-align: center;
    width:100%;
    border: 1px solid #d0d0d0;
    border-top-width: 0px;
    cursor: pointer;
    position: relative;
    color:#616161;
  }
  .weixin-preview .menu-arrow{
    position: absolute;
    left: 50%;
    margin-left: -6px;
  }
  .weixin-preview .arrow_in{
    bottom: -4px;
    display: inline-block;
    width: 0px;
    height: 0px;
    border-width: 6px 6px 0px;
    border-style: solid dashed dashed;
    border-color: #fafafa  transparent transparent;
  }
  .weixin-preview .arrow_out{
    bottom: -5px;
    display: inline-block;
    width: 0px;
    height: 0px;
    border-width: 6px 6px 0px;
    border-style: solid dashed dashed;
    border-color: #d0d0d0 transparent transparent;
  }

  .weixin-preview .menu-item .menu-item-title,.weixin-preview .menu-sub-item .menu-item-title{
    width:100%;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    box-sizing: border-box;
  }


  .weixin-preview .menu-item.current,.weixin-preview .menu-sub-item.current{
    border: 1px solid #44b549;
    background: #fff;
    color:#44b549;
  }
  .weixin-preview .weixin-sub-menu.show{
    display:block;
  }
  .weixin-preview .icon_menu_dot{
    background:url("../../../assets/wechat/index_z354723.png")0px -36px no-repeat;
    width: 7px;
    height: 7px;
    vertical-align: middle;
    display: inline-block;
    margin-right: 2px;
    margin-top: -2px;
  }
  .weixin-preview .icon14_menu_add{
    background: url("../../../assets/wechat/index_z354723.png") 0px 0px no-repeat;
    width: 14px;
    height: 14px;
    vertical-align: middle;
    display: inline-block;
    margin-top: -2px;
  }
  .weixin-preview li:hover .icon14_menu_add{
    background: url("../../../assets/wechat/index_z354723.png") 0px -18px no-repeat;
  }

  .weixin-preview .menu-item:hover{
    color: #000;
  }
  .weixin-preview .menu-sub-item:hover{
    background: #eee;
  }

  .weixin-preview li.current:hover{
    background: #fff;
    color: #44b549;
  }
  /*菜单内容*/
  .weixin-menu-detail{
    width: 600px;
    padding: 0px 20px 5px;
    background-color: #f4f5f9;
    border: 1px solid #e7e7eb;
    float: left;
    min-height: 540px;
  }
  .weixin-menu-detail .menu-name{
    float: left;
    height: 40px;
    line-height: 40px;
    font-size: 18px;
  }
  .weixin-menu-detail .menu-del{
    float: right;
    height: 40px;
    line-height: 40px;
    color: #459ae9;
    cursor:pointer;
  }
  .weixin-menu-detail .menu-input-group{
    width:540px;
    margin:10px 0 30px 0;
    overflow: hidden;
  }
  .weixin-menu-detail .menu-label{
    float: left;
    height: 30px;
    line-height: 30px;
    width:80px;
    text-align: right;
  }
  .weixin-menu-detail .menu-input{
    float:left;
    width: 380px
  }
  .weixin-menu-detail .menu-input-text{
    border: 0px;
    outline: 0px;
    background: #fff;
    width: 300px;
    padding: 5px 0px 5px 0px;
    margin-left: 10px;
    text-indent: 10px;
    height: 35px;
  }
  .weixin-menu-detail .menu-tips{
    color: #8d8d8d;
    padding-top: 4px;
    margin:0;
  }
  .weixin-menu-detail .menu-tips.cursor{
    color: #459ae9;
    cursor: pointer;
  }

  .weixin-menu-detail .menu-input .menu-tips{
    margin:0 0 0 10px;
  }
  .weixin-menu-detail .menu-content{
    padding: 16px 20px;
    border: 1px solid #e7e7eb;
    background-color: #fff;
  }
  .weixin-menu-detail .menu-content .menu-input-group{
    margin: 0px 0 10px 0;
  }
  .weixin-menu-detail .menu-content .menu-label{
    text-align: left;
    width:100px;
  }
  .weixin-menu-detail .menu-content .menu-input-text{
    border: 1px solid #e7e7eb;
  }
  .weixin-menu-detail .menu-content .menu-tips{
    padding-bottom: 10px;
  }

  .weixin-menu-detail .menu-msg-content{
    padding: 0;
    border: 1px solid #e7e7eb;
    background-color: #fff;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-head{
    overflow: hidden;
    border-bottom: 1px solid #e7e7eb;
    line-height: 38px;
    height: 38px;
    padding: 0 20px;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-panel{
    padding: 30px 50px;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-select{
    padding: 40px 20px;
    border: 2px dotted #d9dadc;
    text-align: center;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-select:hover{
    border-color: #b3b3b3;
  }
  .weixin-menu-detail .menu-msg-content strong{
    display: block;
    padding-top: 3px;
    font-weight: 400;
    font-style: normal;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-title{
    float: left;
    width: 310px;
    height: 30px;
    line-height: 30px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-head{
    overflow: hidden;
    border-bottom: 1px solid #e7e7eb;
    line-height: 38px;
    height: 38px;
    padding: 0 20px;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-panel{
    padding: 30px 50px;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-select{
    padding: 40px 20px;
    border: 2px dotted #d9dadc;
    text-align: center;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-select:hover{
    border-color: #b3b3b3;
  }
  .weixin-menu-detail .menu-msg-content strong{
    display: block;
    padding-top: 3px;
    font-weight: 400;
    font-style: normal;
  }
  .weixin-menu-detail .menu-msg-content .menu-msg-title{
    float: left;
    width: 310px;
    height: 30px;
    line-height: 30px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .icon36_common{
    width: 36px;
    height: 36px;
    vertical-align: middle;
    display: inline-block;
  }
  .icon36_common.add_gray{
    background: url("../../../assets/wechat/base_z381ecd.png")0 -2548px no-repeat;
  }
  .icon_msg_sender{
    margin-right: 3px;
    margin-top: -2px;
    width: 20px;
    height: 20px;
    vertical-align: middle;
    display: inline-block;
    background: url("../../../assets/wechat/msg_tab_z25df2d.png")0 -270px no-repeat;

  }

  .weixin-btn-group{
    text-align: center;
    width:950px;
    margin:30px 0px;
    overflow: hidden;
  }
  .weixin-btn-group .btn{
    width: 100px;
    border-radius: 0px;
  }

  #material-list{
    padding:20px;
    overflow-y:scroll;
    height: 558px;
  }
  #news-list{
    padding:20px;
    overflow-y:scroll;
    height: 558px;
  }
  #material-list table{
    width:100%;
  }

</style>
